package editors.gui;
import graphics.system.Color;

import java.util.Map;
import java.util.TreeMap;

import tools.Help;
import tools.Point;
public class GuiHandler {
	private TreeMap<String, GuiObject> guiobjects;
	private Rect bgrect;
	private boolean visible;
	private boolean clicked;
	public GuiHandler() {
		guiobjects = new TreeMap<String, GuiObject>();
		bgrect = new Rect();
		visible = true;
		clicked = false;
	}
	public void handleClick(Point p) {
		if (!visible)
			return;
		if (this.mouseIn(p)) {
			clicked = true;
			for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
				o.getValue().checkClick(p);
			}
		}
	}
	public void drawAll() {
		if (!visible)
			return;
		bgrect.draw(GuiSettings.GUI_BG);
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			o.getValue().draw();
		}
	}
	public void addGuiObject(String name, GuiObject gobj) {
		guiobjects.put(name, gobj);
		float xmin = 10000;
		float xmax = 0;
		float ymin = 10000;
		float ymax = 0;
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			if (o.getValue().rect.getTopLeft().x < xmin)
				xmin = o.getValue().rect.getTopLeft().x;
			if (o.getValue().rect.getBottomRight().x > xmax)
				xmax = o.getValue().rect.getBottomRight().x;
			if (o.getValue().rect.getBottomRight().y < ymin)
				ymin = o.getValue().rect.getBottomRight().y;
			if (o.getValue().rect.getTopLeft().y > ymax)
				ymax = o.getValue().rect.getTopLeft().y;
		}
		bgrect.setCenter(new Point(xmin + (xmax - xmin) / 2, ymin + (ymax - ymin) / 2));
		bgrect.setWidth((xmax - xmin) + 10);
		bgrect.setHeight((ymax - ymin) + 10);
	}
	public boolean isClicked(String objname) {
		if (!visible)
			return false;
		GuiObject gobj = guiobjects.get(objname);
		if (gobj != null) {
			return gobj.isClicked();
		}
		Help.p("Warning: " + objname + " is not a guiobject in this handler!");
		return false;
	}
	public float getSliderValue(String slidername) {
		GuiObject gobj = guiobjects.get(slidername);
		if (gobj != null) {
			try {
				GuiSlider s = (GuiSlider) gobj;
				return s.calcValue();
			} catch (Exception e) {
				Help.p("Warning: " + slidername + " is not a slider!");
			}
		}
		return 0;
	}
	public void applyKeyPress(char c) {
		if (!(32 <= c && c <= 126))
			return;
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			if (o.getValue() instanceof GuiText) {
				((GuiText) o.getValue()).addChar(c);
			}
		}
	}
	public void moveCursor(int d) {
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			if (o.getValue() instanceof GuiText) {
				((GuiText) o.getValue()).moveCursor(d);
			}
		}
	}
	public void cancleEditMode() {
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			if (o.getValue() instanceof GuiText) {
				((GuiText) o.getValue()).stopEdit();
			}
		}
	}
	public void deleteChar() {
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			if (o.getValue() instanceof GuiText) {
				((GuiText) o.getValue()).deleteChar();
			}
		}
	}
	public void setClick(Point p) {
		if (bgrect.isIn(p)) {
			clicked = true;
			for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
				o.getValue().setClick(p);
			}
		}
	}
	public boolean inTextEditMode() {
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			if (o.getValue() instanceof GuiText) {
				if (((GuiText) o.getValue()).isEditing())
					return true;
			}
		}
		return false;
	}
	public boolean isChecked(String chbxname) {
		GuiCheckbox gc = (GuiCheckbox) getObject(chbxname);
		if (gc != null)
			return gc.isChecked();
		Help.p("Warning: no such GuiCheckbox: " + chbxname);
		return false;
	}
	public void labelSetText(String labelname, String m) {
		GuiLabel gl = (GuiLabel) guiobjects.get(labelname);
		if (gl != null)
			gl.setText(m);
		else {
			Help.p("Warning: no such label " + labelname);
		}
	}
	public GuiObject getObject(String name) {
		GuiObject go = guiobjects.get(name);
		if (go == null)
			Help.p("Warning: no such GuiObject: " + name);
		return go;
	}
	public boolean mouseIn(Point mp) {
		return visible && bgrect.isIn(mp);
	}
	public void setSliderValue(String slidername, float v) {
		GuiObject gobj = guiobjects.get(slidername);
		if (gobj != null) {
			try {
				GuiSlider s = (GuiSlider) gobj;
				s.setValue(v);
			} catch (Exception e) {
				Help.p("Warning: " + slidername + " is not a slider!");
			}
		}
	}
	public void setChecked(String chbxname, boolean v) {
		GuiCheckbox gc = (GuiCheckbox) getObject(chbxname);
		if (gc != null) {
			gc.setChecked(v);
			return;
		}
		Help.p("Warning: no such GuiCheckbox: " + chbxname);
	}
	public void editSetText(String editname, String text) {
		GuiText gt = (GuiText) getObject(editname);
		if (gt != null) {
			gt.setText(text);
			return;
		}
		Help.p("Warning: no such GuiCheckbox: " + editname);
	}
	public String getText(String editname) {
		GuiText gt = (GuiText) getObject(editname);
		if (gt != null) {
			return gt.getText();
		}
		Help.p("Warning: no such GuiText: " + editname);
		return "";
	}
	public void setButtonSelected(String buttonname) {
		GuiButton gb = (GuiButton) getObject(buttonname);
		if (gb != null) {
			gb.makeSelected();
			return;
		}
		Help.p("Warning: no such GuiCheckbox: " + buttonname);
	}
	public void unClickAll() {
		clicked = false;
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			o.getValue().setClicked(false);
		}
	}
	public void moveTo(Point p) {
		Point dx = p.sub(bgrect.getTopLeft());
		bgrect.move(dx);
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			o.getValue().move(dx);
		}
	}
	public void moveCenterTo(Point p) {
		Point dx = p.sub(bgrect.getCenter());
		bgrect.move(dx);
		for (Map.Entry<String, GuiObject> o : guiobjects.entrySet()) {
			o.getValue().move(dx);
		}
	}
	public void setVisible(boolean b) {
		visible = b;
	}
	public boolean isVisible() {
		return visible;
	}
	public void setLabelBackground(String labelname, Color color) {
		GuiLabel gl = (GuiLabel) guiobjects.get(labelname);
		if (gl != null)
			gl.setBackgroundColor(color);
		else {
			Help.p("Warning: no such label " + labelname);
		}
	}
	public boolean isClicked() {
		return clicked;
	}
	public float getHeight() {
		return this.bgrect.getHeight();
	}
}
